(* permmult.pas *)

program permmult;

const
   N = 29;
   N_MAX = 256;

var
   perm	: String = '(acfg)(bcd)(aed)(fade)(bgfae)';
   mark	: array[1..N_MAX] of integer;
   out	: array[1..N_MAX] of string[1];
   s	: integer;
      
procedure PrintPerm;
var i : integer;
begin 
   For i:= 1 to N do Write(perm[i]);
   WriteLn();
end;

procedure PrintMark;
var i : integer;
begin 
   For i := 1 to N do Write(mark[i]);
   WriteLn();
end; { PrintMark }

procedure marked(a : char; m : integer);
var i : integer;
begin 
   For i := 1 to N do
      if perm[i] = a then mark[i] := m;
end; { marked }

procedure replace_right_brackets;
var i, j : integer;
begin	 
   for i := 1 to N do
      if mark[i] = 1 then
	 for j := i to N do
	 begin
	    if perm[j] = ')' then perm[j] := perm[i+1];
	       break;
	 end;
end; { replace_right_brackets }

procedure output(a : string);
begin
   out[s] := a;
   s := s+1;
end; { output }

procedure czy_stop;
var i, cnt : integer;
begin
   cnt := 0;
   for i := 0 to N do
      if mark[i] = 1 then cnt := cnt+1;
   if cnt >= N-5 then
      begin
	 exit;
      end;
end; { czy_stop }

var i, j, k : integer;
var START, CURRENT : String;
begin
   marked('(', 1); 
   replace_right_brackets(); 
   PrintPerm();
   PrintMark();
end.
